package com.example.springboottest.example.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestLockService {
    private final Lock lock = new ReentrantLock();
    private final Lock lock2 = new ReentrantLock();

    public void helloLock(String name) {
        try {
            lock.lock();
            System.out.println("helloLock " + name);
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void helloLock2(String name) {
        try {
//            lock2.lock();
            lock.lock();
            System.out.println("helloLock2 " + name);
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
//            lock2.unlock();
        }
    }
}
